<?php
namespace app\job;
use app\common\logic\TronLogic;
use think\Db;
use think\Queue;
use think\queue\Job;
use function Matrix\add;

class Task
{
    public function collection(Job $job, $data){

        //....这里执行具体的任务
        $isJobDone = $this->work($data);

        if ($isJobDone) {
            //如果任务执行成功，记得删除任务
            $job->delete();
        } else {
            if ($job->attempts() > 3) {
                $job->delete();
            }
        }

    }

    public function failed($data){

        // ...任务达到最大重试次数后，失败了
    }

    private function work($data)
    {
        $address = $data['address'];
        $private_key = $data['private_key'];
        $symbol = $data['symbol'];

        $tronLogic = new TronLogic();

        $result = $tronLogic->collectionTrc20($address,$private_key,$symbol);
        if ($result['status'] == 1){
            Db::transaction(function () use ($address){
                Db::name("trc_address")
                    ->where('address',$address)
                    ->update([
                        'is_collect' => 1,
                        'usdt' => 0,
                        'updatetime' => time()
                    ]);
            });

            return true;
        }

        if ($result['status'] == -1) return true;

        if ($result['status'] == 0){
            return false;
        }
    }

}